package com.example.infoserver.controller;

import com.example.infoserver.entity.Testdata;
import com.example.infoserver.repository.TestRepository;
import com.example.infoserver.vo.ResultVO;
import com.example.infoserver.vo.ResultVOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
//@RequestMapping("/api")
public class TestdataController {

    @Autowired
    TestRepository testRepository;

    //用户查询
    @GetMapping("getName/{loginname}")
    public ResultVO getname(@PathVariable("loginname") String loginname){
        List<Testdata> findname = testRepository.findname(loginname);
        return ResultVOUtil.success(findname);
    }
    //获取未读数据长度
    @GetMapping("/getlength/{loginname}")
    public ResultVO getlength(@PathVariable("loginname") String loginname){
        List<Testdata> all = testRepository.findByMsgtypeAndLoginname(null, loginname);
        return ResultVOUtil.success(all.size());
    }
    //分页查询
    @GetMapping("/testFindAll/{page}/{size}/{loginname}")
    public ResultVO testFindAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size,@PathVariable("loginname") String loginname) {
        Pageable pageable = PageRequest.of(page - 1, size, Sort.Direction.ASC, "id");
        List<Testdata> all = testRepository.findByLoginname(loginname,pageable);
        return ResultVOUtil.success(all);
    }

    //分页查询未读状态列表
    @GetMapping("/getstatus/{loginname}/{page}/{size}")
    public ResultVO getstatus( @PathVariable("loginname")String loginname, @PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Pageable pageable = PageRequest.of(page - 1, size, Sort.Direction.ASC, "id");
        List<Testdata> list = testRepository.findByMsgtypeAndLoginname(null,loginname, pageable);
        return ResultVOUtil.success(list);
    }
    //分页查询未读状态列表
    @GetMapping("/getmsgtype/{loginname}/{page}/{size}")
    public ResultVO getmsgtype( @PathVariable("loginname")String loginname, @PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Pageable pageable = PageRequest.of(page - 1, size, Sort.Direction.ASC, "id");
        List<Testdata> list = testRepository.findByMsgtypeAndLoginname("已读",loginname, pageable);
        return ResultVOUtil.success(list);
    }
    //根据id查询内容
    @GetMapping("/getByid/{id}")
    public ResultVO getByid(@PathVariable("id") String id) {
        Optional<Testdata> byId = testRepository.findById(id);
        return ResultVOUtil.success(byId);
    }

    //根据id修改状态
    @PostMapping("/updateStatus/{id}")
    public ResultVO updateStatus(@PathVariable("id") String id) {
        testRepository.updateNameById("已读",id);
        return ResultVOUtil.success();
    }
}
